package cn.zkrt.iotms.module.iot.service.impl;

import cn.zkrt.iotms.module.iot.dao.DeviceAttributeBDao;
import cn.zkrt.iotms.module.iot.dao.DeviceWtblAttributeRelationBDao;
import cn.zkrt.iotms.module.iot.entity.DeviceAttributeB;
import cn.zkrt.iotms.module.iot.entity.DeviceWtblAttributeB;
import cn.zkrt.iotms.module.iot.dao.DeviceWtblAttributeBDao;
import cn.zkrt.iotms.module.iot.entity.DeviceWtblAttributeRelationB;
import cn.zkrt.iotms.module.iot.service.DeviceWtblAttributeBService;
import cn.zkrt.iotms.module.iot.service.DeviceWtblAttributeRelationBService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备物通博联属性(DeviceWtblAttributeB)表服务实现类
 *
 * @author xulinyu
 * @since 2024-10-05 14:58:24
 */
@Service("deviceWtblAttributeBService")
public class DeviceWtblAttributeBServiceImpl extends ServiceImpl<DeviceWtblAttributeBDao,DeviceWtblAttributeB> implements DeviceWtblAttributeBService {
    @Resource
    private DeviceWtblAttributeBDao deviceWtblAttributeBDao;

    private final DeviceWtblAttributeRelationBDao deviceWtblAttributeRelationBDao;

    public DeviceWtblAttributeBServiceImpl(DeviceWtblAttributeRelationBDao deviceWtblAttributeRelationBDao) {
        this.deviceWtblAttributeRelationBDao = deviceWtblAttributeRelationBDao;
    }

    @Override
    public Page<DeviceWtblAttributeB> queryByPage(Page<DeviceWtblAttributeB> page,String bucket, String varName) {
        return deviceWtblAttributeBDao.queryByPage(page,bucket,varName);
    }

    @Override
    public Boolean deleteByIds(List<Long> idList) {
        //绑定了设备属性的物通博联属性不能删除
        LambdaQueryWrapper<DeviceWtblAttributeRelationB> w1 = new LambdaQueryWrapper<>();
        w1.in(DeviceWtblAttributeRelationB::getWtblAttributeId, idList);
        Integer count = deviceWtblAttributeRelationBDao.selectCount(w1);
        if(count > 0) {
            throw new RuntimeException("请解除设备属性关联");
        }
        return this.removeByIds(idList);
    }
}
